#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_data_check_more_table.py 
@version:
@time: 2022/04/07 
"""
import asyncio

from TapClientCore.T07_CaseTemplate.v2.CT03_task.ct_task import *

async_run = asyncio.get_event_loop().run_until_complete


class CTTaskDataCheckMoreTable(CTTask):

    def TASK_TEMPLATE_MORE_TABLE_INITIAL(self, parallel_data):
        data = None
        with allure.step('获取可用表'):
            data = self.lock_and_only_get_table(parallel_data)
        record_id = data.get('id')

        operate = data.pop('operate')
        operate_source = operate.get('source')
        source_info = operate_source.get('source_info')
        source_table = operate_source.get('table')
        operate_target = operate.get('target')
        target_info = operate_target.get('target_info')
        with allure.step('任务名获取'):
            task_name = data.get('task_config').get('config').get('name')
        with allure.step('源数据库初始化'):
            _source = self.init_connector(source_info)
        with allure.step('目标数据库初始化'):
            _target = self.init_connector(target_info)

        # stats check
        _stats = operate.get('stats_check').get('initial')
        _stats_initial = _stats.get('insert')
        # self.drop_more_target_table(_target, source_table)
        async_run(self.async_drop_table(_target, source_table))
        try:
            with allure.step('创建任务'):
                self.create_data_copy_development(data)
            # initial stats insert 校验
            with allure.step('任务状态等待'):
                self.scheduled_task_wait_it_complete(task_name)
            self.func_wait(
                self.get_task_stats_insert,
                self.get_task_status,
                task_name,
                _stats_initial,
            )
        finally:
            # check
            async_run(self.async_tap_more_table_check(
                source_info,
                source_table,
                target_info,
            ))
            self.generate_task_url(task_name, data.get('task_type'))
            logger.info(self.task_url)
            try:
                _source.tapclient_connector.close()
                _target.tapclient_connector.close()
            except Exception as e:
                pass
